package cn.hdustea.shopNavigation.ui.view.main.amusement

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatImageView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import cn.hdustea.shopNavigation.R
import cn.hdustea.shopNavigation.config.ConstantUrl
import cn.hdustea.shopNavigation.entity.BannerEntity
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.activity_amusement.*

class AmusementActivity : AppCompatActivity() {
    private val tabs = arrayOf(
            "文艺咖", "烧脑党", "趣运动",
            "新玩法", "夜行者", "过生日",
            "秋高爽", "约会甜", "家庭暖",
            "附近玩", "网红店", "一人乐",
            "佛系躺")

    companion object {
        fun startActivity(context: Context) {
            context.startActivity(Intent(context, AmusementActivity::class.java))
        }
    }

    private val tabFragmentList = arrayListOf<Fragment>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_amusement)
        initView()
        initBanner()
    }

    private fun initView() {
        for (tab in tabs) {
            tabFragmentList.add(AmusementFragment.newInstance(tab))
        }
        amusement_view_pager.currentItem = 0
        amusement_view_pager.adapter = object : FragmentPagerAdapter(supportFragmentManager,
                BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            override fun getCount(): Int = tabFragmentList.size

            // 创建Fragment
            override fun getItem(position: Int): Fragment = tabFragmentList[position]

            override fun getPageTitle(position: Int): CharSequence? = tabs[position]
        }
        // offscreenPageLimit = 3   是什么意思？？？？？？？？？？
        amusement_view_pager.offscreenPageLimit = 3
        // 将ViewPager关联到TabLayout上
        amusement_tab_layout.setupWithViewPager(amusement_view_pager, false)
    }

    private fun initBanner() {
        val banners = mutableListOf<BannerEntity>()
        for (i in 1..3) {
            val banner = BannerEntity(id = i.toString(),
                    bannerPath = "${ConstantUrl.BASE_URL}/image/banner/beauty$i.jpg",
                    url = null)
            banners.add(banner)
        }
        im_banner.setBannerData(banners)
        im_banner.loadImage { _, model, view, _ ->
            Glide.with(this)       // url获取图片
                    .load((model as BannerEntity).bannerPath)
                    .into(view as AppCompatImageView)
        }
    }
}